# pyqt5
from PyQt5 import QtCore, QtGui, QtWidgets
import psycopg2


class ConnectionInfo:  # 链接到数据库的信息：数据库名称、密码需要调整
    def __init__(self):
        """构造函数，初始化5个用户输入的默认值，根据您的数据库进行调整

        :param self: 相当于c++和java的this
        :return: nothing
        """
        self.host = "localhost"
        self.database = "postgis_31_sample"  # "huining"
        self.user = "postgres"
        self.password = "0"  # "123456"
        self.port = 5432

    # 用户在窗口的文本框中输入值，将这些值赋予成员变量
    def setConnectionInfo(self, host, database, user, password, port):
        """将用户输入的5个值赋给我们的ConnectionInfo对象

        :param self: 相当于c++和java的this
        :param host: 主机名
        :param database: 数据库名
        :param user: 用户名
        :param port: 端口号
        :return: nothing
        """
        self.host = host
        self.database = database
        self.user = user
        self.password = password
        self.port = port

    def connect(self):
        conn = psycopg2.connect(host=self.host,
                                user=self.user,
                                database=self.database,
                                password=self.password,
                                port=self.port)
        return conn


class DatabaseConnectionWindow(QtWidgets.QWidget):
    connSignal = QtCore.pyqtSignal(ConnectionInfo)  # 初始化qt信号

    def __init__(self, parent=None):
        super(DatabaseConnectionWindow, self).__init__(parent)

        self.__initUI()

        connInfo = ConnectionInfo()
        # 为5个编辑框里的文字赋初值
        self.hostEdit.setText(connInfo.host)
        self.databaseEdit.setText(connInfo.database)
        self.portEdit.setText(str(connInfo.port))
        self.passwordEdit.setText(connInfo.password)
        self.userEdit.setText(connInfo.user)

    # load_DBdata.py里也有这个函数，还没弄明白
    def __retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "数据库配置"))
        self.connectButton.setText(_translate("Form", "添加连接"))
        self.label.setText(_translate("Form", "host"))
        self.label_3.setText(_translate("Form", "database"))
        self.label_5.setText(_translate("Form", "port"))
        self.label_4.setText(_translate("Form", "password"))
        self.label_2.setText(_translate("Form", "user"))

    def __emitConn(self):
        """将用户新输入的5个值赋给一个新的ConnectionInfo对象

        :param self: 相当于c++和java的this
        :return: nothing
        """
        newConn = ConnectionInfo()
        newConn.setConnectionInfo(self.hostEdit.text(),
                                  self.databaseEdit.text(),
                                  self.userEdit.text(),
                                  self.passwordEdit.text(),
                                  int(self.portEdit.text()))  # 强制类型转换为integer
        self.connSignal.emit(newConn)  # 发出信号
        self.close()  # 关闭窗口

    def __initUI(self):
        """初始化UI界面，大部分静态窗口的代码量可以用Qt designer来做

        :param self: 相当于c++和java的this
        :return: nothing
        """
        self.setObjectName("Form")
        self.resize(400, 237)
        self.connectButton = QtWidgets.QPushButton(self)
        self.connectButton.setGeometry(QtCore.QRect(160, 190, 93, 28))
        self.connectButton.setObjectName("connectButton")
        self.connectButton.clicked.connect(self.__emitConn)
        self.widget = QtWidgets.QWidget(self)
        self.widget.setGeometry(QtCore.QRect(170, 30, 173, 139))
        self.widget.setObjectName("widget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.widget)
        self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_2.setVerticalSpacing(8)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.hostEdit = QtWidgets.QLineEdit(self.widget)
        self.hostEdit.setObjectName("hostEdit")
        self.gridLayout_2.addWidget(self.hostEdit, 0, 0, 1, 1)
        self.userEdit = QtWidgets.QLineEdit(self.widget)
        self.userEdit.setObjectName("userEdit")
        self.gridLayout_2.addWidget(self.userEdit, 1, 0, 1, 1)
        self.passwordEdit = QtWidgets.QLineEdit(self.widget)
        self.passwordEdit.setObjectName("passwordEdit")
        self.gridLayout_2.addWidget(self.passwordEdit, 2, 0, 1, 1)
        self.portEdit = QtWidgets.QLineEdit(self.widget)
        self.portEdit.setObjectName("portEdit")
        self.gridLayout_2.addWidget(self.portEdit, 3, 0, 1, 1)
        self.databaseEdit = QtWidgets.QLineEdit(self.widget)
        self.databaseEdit.setObjectName("databaseEdit")
        self.gridLayout_2.addWidget(self.databaseEdit, 4, 0, 1, 1)
        self.widget1 = QtWidgets.QWidget(self)
        self.widget1.setGeometry(QtCore.QRect(70, 30, 66, 137))
        self.widget1.setObjectName("widget1")
        self.gridLayout = QtWidgets.QGridLayout(self.widget1)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setHorizontalSpacing(7)
        self.gridLayout.setVerticalSpacing(10)
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtWidgets.QLabel(self.widget1)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.label_3 = QtWidgets.QLabel(self.widget1)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 4, 0, 1, 1)
        self.label_5 = QtWidgets.QLabel(self.widget1)
        self.label_5.setTextFormat(QtCore.Qt.AutoText)
        self.label_5.setObjectName("label_5")
        self.gridLayout.addWidget(self.label_5, 3, 0, 1, 1)
        self.label_4 = QtWidgets.QLabel(self.widget1)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 2, 0, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.widget1)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)

        self.__retranslateUi(self)

        QtCore.QMetaObject.connectSlotsByName(self)  # 这是什么呢

